home *** CD-ROM | disk | FTP | other *** search
- Path: news2.noc.netcom.net!news
- From: Tarang Deshpande <tarang@willows.com>
- Newsgroups: comp.lang.c
- Subject: Re: Newbie needs help w/ recursion
- Date: Tue, 19 Mar 1996 16:54:13 -0800
- Organization: NETCOM Network Operations
- Message-ID: <314F5735.6E96@willows.com>
- References: <4ikq6p$io1@impsets.dash.com>
- NNTP-Posting-Host: daffy.willows.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
-
- Jim Bosshardt wrote:
- >
- > Hi, I am new and am about to pull my hair out over the supposedly simple
- > recursion problem I have. The function is to take a number and raise it
- > to a neg or pos power and return the value to main(). I need to take
- > the following and make it a recursive function...
- >
- > double power (double a, float b)
-
-
- Note the change that I've made to the parameter list.
-
- double power ( double a, long b )
- {
-
- if ( ! a )
- /*--- This is actually an error ---*/
- return ( 0 );
-
- if ( ( ! b ) || ( b == -1 ) )
- return ( 1 );
-
- if ( b == 1 )
- return ( a );
-
- if ( b > 0 )
- return ( a * power ( a, b - 1 ) );
-
- if ( b < 0 )
- return ( 1 / ( a * power ( a, b + 1 ) ) );
-
- }
-
-
- Of course you could just use the pow function instead.
-